package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberMapper;
import com.itheima.dao.OrderMapper;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfoVo;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @outhor
 * @date 2020/2/5 14:57
 */

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public Result submit(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();//体检日期
        String name = orderInfoVo.getName();//体检日期
        String telephone = orderInfoVo.getTelephone();//手机号
        String idCard = orderInfoVo.getIdCard();//身份证号
        Integer sex = orderInfoVo.getSex();//性别
        String orderType = orderInfoVo.getOrderType();//预约类型
        Integer setmealId = orderInfoVo.getSetmealId();//套餐id

        //根据体检日期查询当天是否有预约,以及预约情况
        OrderSetting orderSetting = orderSettingMapper.findByDate(orderDate);
        //当天无预约将返回"所选日期不能进行体检预约"
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        //当天预约满则返回"预约已满"
        if (number<=reservations){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //上面都通过(有预约并预约没满),通过手机号查询该体检人是否是会员
        Member member = memberMapper.findBytelephone(telephone);
        //如果不是会员,则添加会员(录入信息)
        if (member==null){
            member = new Member();
            member.setName(name);
            member.setSex(sex.toString());
            member.setPhoneNumber(telephone);
            member.setIdCard(idCard);
            member.setRegTime(new Date());
            memberMapper.add(member);
        }
        //通过"体检日期","套餐id","memberId"查询该日期该人的预约情况
        Integer memberId = member.getId();
        Order queryParam = new Order();
        queryParam.setOrderDate(orderDate);
        queryParam.setSetmealId(setmealId);
        queryParam.setMemberId(memberId);
        Order order = orderMapper.findByOrder(queryParam);
        if (!(order==null)){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }
        order=new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setSetmealId(setmealId);
        order.setOrderStatus(order.ORDERSTATUS_NO);
        orderMapper.add(order);

        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingMapper.editReservations(orderSetting);

        //要返回order对象，页面用到了订单id
        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
    }

    @Override
    public Order findById(String id) {
        Order order = orderMapper.findById(id);
        return order;
    }
}
